﻿package com.vinyl.controls
{
	import flash.events.*;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.display.MovieClip;
	import com.vinyl.controls.Button;
	
	public class RadioGroup extends EventDispatcher 
	{
		public static var EVENT_CHANGED: String = 'radiogroup_changed';
		
		private var selected: Button;
		
		public function setSelected( button: Button ): void
		{
			if( selected == button ) 
			{
				return;
			}
			activateButton( button );
		}
		
		public function getSelected(): Button
		{
			return selected;
		}
		
		public function onButtonChanged( evt ): void
		{
			var button: Button = Button( evt.target );
			if( button.getValue() )
				activateButton( button );
		}
		
		private function activateButton( button: Button ): void
		{
			if( selected )
			{
				selected.setEnabled( true );
				selected.setValue( false );
				selected = null;
			}
			
			button.setEnabled( false ); 
			button.setValue( true );
			
			selected = button;
			
			dispatchEvent( new Event( EVENT_CHANGED ) );
		}
	}
}


